const app = getApp()
const promisify = require("../../utils/promisify")

Page({
  data: {
    activityId: undefined,
    organizationId: undefined,
    activityImg: "",
    activityName: "",
    organizationImg: "",
    organizationName: "",
    breif: [],
    time: "",
    detail: [],
    studentNumber: undefined,
    studentName: undefined,
    studentClass: undefined,
    telephone: undefined
  },
  onLoad: function (options) {
    let that = this
    that.setData({
      activityId: options.activityId
    })
    promisify(wx.request)({
      url: `https://www.cumthing.world/cumthing/activity-info/detail` + app.ToParams({
        activityId: that.data.activityId
      }),
      method: "GET",
      header: {
        'Content-Type': 'application/json' // 默认值
      }
    }).then(
      data => {
        data = data.data
        console.log(data)
        that.setData({
          activityName: data.activityName,
          activityImg: data.activityPost,
          organizationImg: data.organizationImg,
          organizationName: data.organizationName,
          organizationId: data.organizationId,
          brief: [data.activityDetail],
          time: data.activityTime,
          detail: [data.activityDescription]
        })
      }
    )
  },
  preview: function (e) {
    let that = this
    let current = e.target.dataset.url
    let urls = [current]
    console.log(current)
    wx.previewImage({
      current: current,
      urls: urls
    })
  },
  bindInput: function (e) {
    /**
     * 为输入框绑定数据
     */
    let data = e.target.dataset.data
    this.setData({
      [data]: e.detail.value
    })
  },
  checkPhone: function () {
    var reg = /^((1[3-9][0-9])+\d{8})$/;
    if (!reg.test(this.data.telephone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    return true
  },
  checkNumber: function () {
    let that = this
    var reg = /^\d{8}$/
    if (!reg.test(that.data.studentNumber)) {
      wx.showToast({
        title: "请输入正确的学号",
        icon: "none",
        duration: 2000
      })
      return false
    }
    return true
  },
  checkName: function () {
    let that = this
    if (!that.data.studentName) {
      wx.showToast({
        title: "请输入姓名",
        icon: "none",
        duration: 2000
      })
      return false
    }
    return true
  },
  checkClass: function () {
    if (!this.data.studentClass) {
      wx.showToast({
        title: "请输入班级",
        icon: "none",
        duration: 2000
      })
      return false
    }
    return true
  },
  sign: function () {
    let that = this
    if (app.loginFirst() && that.checkNumber() && that.checkName() && that.checkClass() && that.checkPhone()) {
      promisify(wx.request)({
        url: `https://www.cumthing.world/cumthing/activity-apply/apply` + app.ToParams({
          activityId: that.data.activityId,
          userId: app.globalData.userId,
          studentNumber: that.data.studentNumber,
          studentName: that.data.studentName,
          studentClass: that.data.studentClass,
          telephone: that.data.telephone
        }),
        method: "POST",
        header: {
          'Authorization': app.globalData.token
        },
      }).then(
        data => {
          if (data.data === "恭喜您报名成功") {
            wx.showToast({
              title: "报名成功",
              icon: "none",
              duration: 2000
            })
          } else if (data.code === 403) {
            wx.showToast({
              title: "已报名",
              icon: "none",
              duration: 2000
            })
          }
        },
        err => {
          console.log(err)
        }
      )
    }
  }
})